// pages/myPan/crowdfunding/crowdfunding.js
import api from '../../../api/api.js'
import load from '../../templates/loader/loader.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 500,
    itemData: [],
    scrollLeft: 0,
    currentTab: 0,
    index: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //  换算单个tab宽度
    var w_width = wx.getSystemInfoSync().windowWidth;
    var rpx = w_width / 750;
    var tab_width = 165 * rpx;
    that.setData({
      tab_width: tab_width,
      windowWidth: w_width
    });
    //初始化载入等待动画
    that.loader = load.loader(that, {
      data: "loader",
      toast: true,
      size: 50,
      borderWidth: 3
    });
    //获取用户信息
    api.getUser({
      done: (user) => {
        console.log("user", user)
        that.setData({
          userInfo: user,
        });
        that.loadData()
      }
    })

  },
  loadData: function () {
    var that = this
    var jsonStr = '{"REQ_HEAD":{"SIGN":"8495897b17c2813da1ef5e8c7ca45e21"},"REQ_BODY":{"userID":"' + that.data.userInfo.id + '"}}'
    that.loader.open();
    api.getCrowdfundingItem({
      method: 'POST',
      data: {
        REQ_MESSAGE: jsonStr
      },
      success: (res) => { 
        that.loader.close()
        if (res.rows) {
          res.rows.forEach(function (item) {
            if (item.start_time) {
              // var s = item.start_time;
              // var a = s.split(/[^0-9]/);
              item.start_time = that.dateFtt("yyyy-MM-dd hh:mm:ss", new Date(item.start_time));
              item.payment_time = that.dateFtt("yyyy-MM-dd hh:mm:ss", new Date(item.payment_time));
            }
          });
          that.setData({
            itemData: res.rows
          })
        }
      }
    })
  },
  dateFtt: function (fmt, date) {
    var o = {
      "M+": date.getMonth() + 1, //月份
      "d+": date.getDate(), //日
      "h+": date.getHours(), //小时
      "m+": date.getMinutes(), //分
      "s+": date.getSeconds(), //秒
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
      if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  },
  switchTab: function (e) {
    var self = this;
    var current = e.detail.current;
    var scrollLeft = self.data.tab_width * (current + 1);
    /**
     * tab自动切换
     */
    // if (scrollLeft > self.data.windowWidth){
    //     var index = parseInt(self.data.index);
    //     index++;
    //     scrollLeft = self.data.tab_width * index;

    //     console.log(index)
    //     self.setData({
    //         scrollLeft: scrollLeft,
    //         index: index
    //     });
    // }
    self.setData({
      currentTab: e.detail.current,
    });
  },
  /**
   * 动态tab切换
   */
  swiperTab: function () {
    var self = this;

  },
  switchSwiper: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.index
    })
  },
  getRefund: function (e) {
    var that = this;
    var json = '{"REQ_HEAD":{ "SIGN":"null" },"REQ_BODY": { "orderId": "' + e.currentTarget.dataset.orderid + '","infoId":"0", "userId": "' + that.data.userInfo.id + '" }}';

    wx.showModal({
      title: '提示',
      content: '确定退出众筹？',
      confirmColor: "#e30c26",
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            mask: true
          })
          api.getRefund({
            method: 'POST',
            data: {
              REQ_MESSAGE: json
            },
            complete: (res) => {
              wx.hideLoading()
              if (res.statusCode == 200) {
                if (res.data.REP_BODY.RSPCOD == "000000") {
                  wx.showToast({
                    title: '提交成功',
                    icon: 'success',
                    duration: 3000
                  })
                  that.data.itemData.forEach(function (item) {
                    if (e.currentTarget.dataset.orderid == item.tot_id) {
                      item.payment_status = 2;
                    }
                  });
                  that.setData({
                    itemData: that.data.itemData
                  })
                } else {
                  wx.showToast({
                    title: res.data.REP_BODY.RSPCOD || '退款失败',
                    image: '/image/ico/error-ico.png',
                    duration: 2000
                  })
                }
              } else {
                wx.showToast({
                  title: '退款失败',
                  image: '/image/ico/error-ico.png',
                  duration: 2000
                })
              }
            }
          })
        }
      }
    })
  }
})